﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlServerCe;
using System.Collections;

namespace CPTECH.Repository
{
    class FilialDB: BaseDB
    {
        private static FilialDB instance;

        private FilialDB() { }

        public static FilialDB getInstance()
        {
            if (instance == null)
            {
                instance = new FilialDB();
            }
            return instance;
        }

        //seleciona as OS's distintas dentro das filiais.
        public ArrayList selectDistinctOS()
        {
            try
            {
                this.open();
                SqlCeCommand command = new SqlCeCommand("SELECT DISTINCT OS FROM TB_FILIAL;", this.conn);
                SqlCeDataReader rdr = command.ExecuteReader();
                ArrayList result = new ArrayList();

                while (rdr.Read())
                {
                    result.Add(rdr.GetString(0));
                }

                return result;
            }
            finally
            {
                this.close();
            }
        
        }

        //seleciona todas as Filiais se suas descrições.
        public ArrayList selectAllFiliais()
        {
            try
            {
                this.open();
                SqlCeCommand command = new SqlCeCommand("select cd_fis_filial, ds_filial from TB_FILIAL;", this.conn);
                SqlCeDataReader rdr = command.ExecuteReader();
                ArrayList result = new ArrayList();

                while (rdr.Read())
                {
                    result.Add(rdr.GetInt64(0).ToString() + " = " + rdr.GetString(1));
                }

                return result;
            }
            finally
            {
                this.close();
            }
        }

        //inseri nova filial.
        public bool insertNewFilial(string os, Int64 codigoFilial, string descFilial, string dtRegistro, byte enviado)
        {
            bool resposta = false;
            try
            {
                this.open();
                SqlCeCommand command = new SqlCeCommand("insert into TB_FILIAL values('" + os + "','" + codigoFilial + "','" + descFilial +
                                                        "','" + dtRegistro + "','" + enviado + "');", this.conn);
                command.ExecuteNonQuery();
                resposta = true;
            }
            catch (FormatException)
            {
                resposta = false;
            }
            finally
            {
                this.close();
            }
            return resposta;        
        }

        //checa se código filial já existe.
        public bool checkCodFilial(Int64 codFilial)
        {
            bool result = false;
            try
            {
                this.open();
                SqlCeCommand command = new SqlCeCommand("select * from TB_FILIAL where cd_fis_filial =@codFilial;", this.conn);
                command.Parameters.AddWithValue("@codFilial", codFilial);
                SqlCeDataReader rdr = command.ExecuteReader();
                while (rdr.Read())
                {
                    result = true;
                }
            }
            finally
            {
                this.close();
            }
            return result;
        }


    }
}
